//

// (  )
//

package com.tools.acceleratecore.xlib.tool.impl;

import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import com.tools.acceleratecore.xlib.XLibFactory;
import com.tools.acceleratecore.xlib.tool.intf.ISMSObserver;
import com.tools.acceleratecore.xlib.tool.intf.ISMSObserverListener;
import com.tools.acceleratecore.xlib.xlib.impl.XObserver;
import java.util.Iterator;

public class SMSObserver extends XObserver<ISMSObserverListener> implements ISMSObserver {
    private Context mContext = null;
    private boolean mInited = false;
    private ContentObserver mContentObserver = null;

    public SMSObserver() {
        this.mContext = XLibFactory.getApplication();
        this._init();
    }

    private void _init() {
        this.mContentObserver = new ContentObserver(new Handler()) {
            public void onChange(boolean selfChange) {
                synchronized(SMSObserver.this.mListListener) {
                    Iterator var4 = SMSObserver.this.getListListener().iterator();

                    while(var4.hasNext()) {
                        ISMSObserverListener listener = (ISMSObserverListener)var4.next();
                        listener.onChange(selfChange);
                    }

                }
            }
        };
    }

    public boolean startListen() {
        if (this.mInited) {
            return false;
        } else {
            this.mInited = true;
            this.mContext.getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, this.mContentObserver);
            return true;
        }
    }

    public void stopListen() {
        if (this.mInited) {
            this.mInited = false;
            this.mContext.getContentResolver().unregisterContentObserver(this.mContentObserver);
        }
    }

    public int getNewSmsCount() {
        int nCount = 0;
        Cursor cursor = this.mContext.getContentResolver().query(Uri.parse("content://sms"), (String[])null, "type = 1 and read = 0", (String[])null, (String)null);
        if (cursor != null) {
            nCount = cursor.getCount();
            cursor.close();
        }

        return nCount;
    }
}
